/**
 * 项目名：化工企业安全生产信息化管理平台
 * 日期：    2020-03-01 23:13:58
 * Copyright (c) 2015- -版权所有
 */

package com.joysuch.wwyt.edu.controller;

import cn.zlg.common.springmvc.validate.constraints.NotNull;
import com.joysuch.wwyt.core.bean.*;
import com.joysuch.wwyt.core.common.ajax.ResultBean;
import com.joysuch.wwyt.core.common.util.PageDataUtils;
import com.joysuch.wwyt.core.service.SimpleSelectListService;
import com.joysuch.wwyt.edu.bean.*;
import com.joysuch.wwyt.edu.bean.EduCourseCoursewareListBean.SearchEduCourseCourseware;
import com.joysuch.wwyt.edu.bean.EduCourseListBean.SearchEduCourse;
import com.joysuch.wwyt.edu.entity.EduCourse;
import com.joysuch.wwyt.edu.service.EduCourseService;
import com.joysuch.wwyt.edu.service.EduCoursewareService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.apache.shiro.util.Assert;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.validation.Valid;
import java.util.Arrays;
import java.util.List;
import java.util.stream.Collectors;

@RestController
@RequestMapping("/edu/course")
@Api(tags = "/edu/course 课程中心")
public class EduCourseController {

    private static final Logger logger = LoggerFactory.getLogger(EduCourseController.class);

    @Autowired
    private EduCourseService eduCourseService;
    @Autowired
    private EduCoursewareService coursewareService;
    @Autowired
    private SimpleSelectListService simpleSelectListService;

    @RequiresPermissions("edu:course:add")
    @RequestMapping("add")
    public Object add(@Valid @RequestBody EduCourseEditBean entity) {
        eduCourseService.add(entity);
        return ResultBean.success("");
    }

    @RequiresPermissions("edu:course:update")
    @RequestMapping("update")
    public Object update(@Valid @RequestBody EduCourseEditBean entity) {
        if (entity.getId() == null) {
            return ResultBean.fail(102, "未指定主键");
        }
        eduCourseService.update(entity);
        return ResultBean.success("");
    }

    //  @RequiresPermissions("edu:course")
    @RequestMapping("page")
    public Object page(Pageable page, SearchEduCourse condition) {
        page = PageDataUtils.addOrderByDesc(page, "id");
        Page<EduCourseListBean> data = eduCourseService.findByPage(page, condition);
        return ResultBean.pageData(data.getContent(), data.getTotalElements());
    }

    //  @RequiresPermissions("edu:course")
    @RequestMapping("select-page")
    public Object selectPage(Pageable page, SearchEduCourse condition) {
        page = PageDataUtils.addOrderByDesc(page, "id");
        Page<EduCourseListBean> data = eduCourseService.findByPage(page, condition);
        return ResultBean.pageData(data.getContent(), data.getTotalElements());
    }

    @GetMapping("statistics/page")
    @ApiOperation(value = "web 课程统计列表查询-分页查询", response = EduCourseVo.class)
    @ResponseBody
    public Object courseList(Pageable page, SearchEduCourse condition) {
        page = PageDataUtils.addOrderByDesc(page, "id");
        Page<EduCourseVo> data = eduCourseService.coursePage(page, condition);
        return ResultBean.pageData(data.getContent(), data.getTotalElements());
    }

    @GetMapping("statistics/export")
    @ApiOperation(value = "课程统计-导出")
    public void export(SearchEduCourse condition, HttpServletRequest request, HttpServletResponse response) {
        eduCourseService.courseExport(condition, request, response);
    }

    @RequestMapping("simple-list")
    @ApiOperation(value = "课程名称查询", response = SimpleSelectListBean.class)
    public Object simpleList() {
        List<SimpleSelectListBean> data = simpleSelectListService.list(EduCourse.class, "name");
        return ResultBean.success(data);
    }

    @RequestMapping("v2-simple-list")
    @ApiOperation(value = "课程名称查询", response = SimpleSelectListBean.class)
    public Object newSimpleList(String name) {
        List<CourseBean> data = eduCourseService.findAll(name);
        return ResultBean.success(data);
    }

    //  @RequiresPermissions("edu:course")
    @RequestMapping("courseware-page")
    public Object coursewarePage(Pageable page, SearchEduCourseCourseware condition) {
        Assert.notNull(condition.getCourseId(), "课程ID不能为空");
        Page<EduCoursewareListBean> data = eduCourseService.findCoursewareByPage(page, condition);
        return ResultBean.pageData(data.getContent(), data.getTotalElements());
    }

    @GetMapping("coursewareByList")
    @ApiOperation("课程id查询所属课件")
    public ResultBean coursewareList(Long courseId) {
        List<SimpleListBean> data = eduCourseService.findCoursewareByList(courseId);
        return ResultBean.success(data);
    }

    @RequestMapping("batch-add-courseware")
    public Object batchAddCourseware(Long courseId, String coursewareIds) {
        String[] split = coursewareIds.split(",");
        eduCourseService.batchAddCourseware(courseId,
                Arrays.asList(split).stream().map(Long::parseLong).collect(Collectors.toList()));
        return ResultBean.success(null);
    }

    @RequestMapping("delete-courseware")
    public Object deleteCourseware(Long courseId, Long coursewareId) {
        eduCourseService.deleteCourseware(courseId, coursewareId);
        return ResultBean.success(null);
    }

    @RequiresPermissions("edu:course:delete")
    @RequestMapping("delete")
    public Object delete(Long id) throws Exception {
        eduCourseService.delete(id);
        return ResultBean.success("");
    }

    //  @RequiresPermissions("edu:course")
    @RequestMapping("get")
    public Object getById(@Valid @NotNull(field = "id") Long id) {
        return ResultBean.success(eduCourseService.getById(id));
    }

    //  @RequiresPermissions("edu:course")
    @RequestMapping("courseware-type-list")
    @ApiOperation(value = "课件类型查询", response = SimpleSelectListBean.class)
    public Object queryCoursewareTypeListOfCourse(Long courseId) {
        List<SimpleSelectListBean> typeList = eduCourseService.queryCoursewareTypeListOfCourse(courseId);
        return ResultBean.success(typeList);
    }

    //  @RequiresPermissions("edu:course")
    @RequestMapping("courseware-list")
    @ApiOperation(value = "课件查询", response = EduCoursewareDetailBean.class)
    public Object queryCoursewareTypeListOfCourse(Long courseId, Long coursewareTypeId, Integer size, Integer page) {
        Pageable pageable = null;
        if (size != null && page != null) {
            pageable = PageRequest.of(page, size);
        }
        Page<EduCoursewareDetailBean> typeList = eduCourseService.queryCoursewareList(courseId, coursewareTypeId,
                pageable);
        return ResultBean.pageData(typeList);
    }

}
